package com.zhouyibike.uu.ui.activity.mybike;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MyBicycleInterfaceResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.adapter.MytablayoutvpAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.Util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zy on 2017/10/11.
 */
public class MyBikeinfo1Fragment extends Fragment implements View.OnClickListener {
    private TabLayout tabs;
    private ViewPager viewPager;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();
    private MyBikeFragment f1, f2, f3, f4, f5, f6, f7, f8;
    private MytablayoutvpAdapter adpater;
    public TextView mybikenumtv, zhuanrangzhongnumtv, zuorishouyitv;
    private ImageView rengouiv, haoyouzhuanzengiv, ershouiv, chexiaozr;
    private TextView chakanxiangqingtv;
    public int totalnum;//单车总量
    private int canbacknum;//转让中单车总量
    private double zhuanzengprice=0;//每辆车手续费
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        View view = View.inflate(getActivity(), R.layout.fragment_mybikeinfo1, null);
        //  type=getIntent().getStringExtra("type");
        initView(view);
        initdata();
        getMsg();
        // ((BaseActivity) getActivity()).toastShow(b.getString("type"));
        initlistener();
        reflex(tabs);
        return view;
    }

    private void initlistener() {
        chakanxiangqingtv.setOnClickListener(this);
        rengouiv.setOnClickListener(this);
        haoyouzhuanzengiv.setOnClickListener(this);
        ershouiv.setOnClickListener(this);
        chexiaozr.setOnClickListener(this);
    }

    private void setmsgdata(MyBicycleInterfaceResult model) {
//        mybikenum.setText(mybikeinfo.getData().getMyBicCount() + "辆");
//        totalnum = mybikeinfo.getData().getMyBicCount();
//        mybikezhuanrangnum.setText(mybikeinfo.getData().getMyBicTransferCount() + "辆");
//        canbacknum = mybikeinfo.getData().getMyBicTransferCount();
//        qiugoubike.setText(0 + "辆");
        mybikenumtv.setText(model.getData().getMyBicCount() + "辆");
        zhuanrangzhongnumtv.setText(model.getData().getMyBicTransferCount() + "辆");
        zhuanzengprice=model.getData().getTransferFreePrice();
        ((MybikeActivity6)getActivity()).zhuanzengshouxufei=zhuanzengprice;
        totalnum = model.getData().getMyBicCount();
        ((MybikeActivity6)getActivity()).cansellnum=totalnum;
        canbacknum = model.getData().getMyBicTransferCount();
    }

    private void initdata() {
        mTitle.add("七日十辆单车收益");
        mTitle.add("我购买的单车收益");
        f1 = new MyBikeFragment();
        f2 = new MyBikeFragment();
//        f3 = new MyBikeFragment();
//        f4 = new MyBikeFragment();
        mFragment.add(f1);
        mFragment.add(f2);
        adpater = new MytablayoutvpAdapter(getChildFragmentManager(), mTitle, mFragment);
        viewPager.setAdapter(adpater);
        //为TabLayout设置ViewPager
        tabs.setupWithViewPager(viewPager);
        //使用ViewPager的适配器
        tabs.setTabsFromPagerAdapter(adpater);
        tabs.setTabTextColors(getResources().getColor(R.color.text_main_9), getResources().getColor(R.color.radio_button_selected_color));//字体颜色
        tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.radio_button_selected_color));
        for (int i = 0; i < mTitle.size(); i++) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", i);
            bundle.putString("token", getActivity().getSharedPreferences("zhouyidanche", getActivity().MODE_PRIVATE).getString("token", ""));
            mFragment.get(i).setArguments(bundle);
        }
    }

    private void initView(View head) {
//        zuorishouyi = head.findViewById(R.id.zuorishouyi);
//        mybike_buy = (Button) head.findViewById(R.id.mybike_buy);
//        chexiaozhuangrang = (TextView) head.findViewById(R.id.chexiaozhuangrang);
//        chakanxiangqing = (TextView) head.findViewById(R.id.chakanxiangqing);
//        qiugoubike = (TextView) head.findViewById(R.id.qiugoubike);
//        mybikenum = (TextView) head.findViewById(R.id.mybikenum);
//        mybikezhuanrangnum = (TextView) head.findViewById(R.id.mybikezhuanrangnum);
        mybikenumtv = head.findViewById(R.id.mybikenum);
        zhuanrangzhongnumtv = head.findViewById(R.id.mybikezhuanrangnum);
        zuorishouyitv = head.findViewById(R.id.zuorishouyi);
        viewPager = (ViewPager) head.findViewById(R.id.mybikevp);
        tabs = (TabLayout) head.findViewById(R.id.mybiketablayout);
        chakanxiangqingtv = head.findViewById(R.id.chakanxiangqing);
        rengouiv = head.findViewById(R.id.rengouiv);
        haoyouzhuanzengiv = head.findViewById(R.id.zhuanzengiv);
        ershouiv = head.findViewById(R.id.zhuanrangiv);
        chexiaozr = head.findViewById(R.id.chexiaoiv);
        Util.setLviewwh(rengouiv, Integer.parseInt("1"), Integer.parseInt("1"), 0.25);
        Util.setLviewwh(haoyouzhuanzengiv, Integer.parseInt("1"), Integer.parseInt("1"),  0.25);
        Util.setLviewwh(ershouiv, Integer.parseInt("1"), Integer.parseInt("1"),  0.25);
        Util.setLviewwh(chexiaozr, Integer.parseInt("1"), Integer.parseInt("1"),  0.25);
    }

    public void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", ((BaseActivity) getActivity()).preferences.getString("token", ""));
//        showProgressDialog();
        ((BaseActivity) getActivity()).addSubscription(((BaseActivity) getActivity()).apiStores.MyBicycleInterface(params),
                new ApiCallback<MyBicycleInterfaceResult>() {

                    @Override
                    public void onSuccess(MyBicycleInterfaceResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            setmsgdata(model);
                        } else if (model.getStatus() == 508 || model.getStatus() == 506 || model.getStatus() == 307) {
                            ( getActivity()).setResult(2501);
                            ( getActivity()).finish();
                        } else {
                            ((BaseActivity) getActivity()).toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onFinish() {
//                        getMsg2();
//                        dismissProgressDialog();
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.bt:
////                rlybt.setVisibility(View.GONE);
//                break;
            case R.id.chakanxiangqing:
                Intent intent5 = new Intent(getActivity(), MybikeinfoActivity.class);
                intent5.putExtra("cansellnum", totalnum);
                startActivityForResult(intent5, 500);
                break;
            case R.id.zhuanzengiv://转赠
//                Intent intent3 = new Intent(getActivity(), ZhuanZengActivity.class);
//                intent3.putExtra("cansellnum", totalnum);
//                startActivityForResult(intent3, 300);
                if (((BaseActivity) getActivity()).preferences.getBoolean("isfirstzhuanzeng", true)) {//第一次
                    SharedPreferences.Editor editor = ((BaseActivity) getActivity()).preferences.edit();
                    editor.putBoolean("isfirstzhuanzeng", false);
                    editor.commit();
                    Intent intent3 = new Intent(getActivity(), FirstZhuanzengActivity.class);
                    intent3.putExtra("cansellnum", totalnum);
                    intent3.putExtra("zhuanzengprice", zhuanzengprice);
                    startActivityForResult(intent3, 301);
                } else {//不是第一次
//                    Intent intent3 = new Intent(getActivity(), FirstZhuanzengActivity.class);
//                    intent3.putExtra("cansellnum", totalnum);
//                    intent3.putExtra("zhuanzengprice", zhuanzengprice);
//                    startActivityForResult(intent3, 301);
                    Intent intent3 = new Intent(getActivity(), HaoyouZhuanzengActivity.class);
                    intent3.putExtra("cansellnum", totalnum);
                    intent3.putExtra("zhuanzengprice", zhuanzengprice);
                    startActivityForResult(intent3, 302);
                }
                break;
            case R.id.chexiaoiv://撤销转让
                Intent intent4 = new Intent(getActivity(), ZhuanRangBackActivity.class);
                intent4.putExtra("cxzr", canbacknum);
                startActivityForResult(intent4, 400);
                break;
            case R.id.rengouiv:
                //  Intent intent = new Intent(MyBikeActivity.this, BuyBikeActivity.class);
                Intent intent = new Intent(getActivity(), RengouActivity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.zhuanrangiv:
                Intent intent1 = new Intent((MybikeActivity6) getActivity(), ZhuanRangActivity.class);
                intent1.putExtra("zrnum", totalnum);
                startActivityForResult(intent1, 10010);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 1) {

        } else if (requestCode == 10010 && resultCode == 1) {//直接转让
            getMsg();
            ((MybikeActivity6)getActivity()).f2.datafalsh();
        } else if (requestCode == 400 && resultCode == 1) {//撤销转让
            getMsg();
            ((MybikeActivity6)getActivity()).f2.datafalsh();
        } else if (requestCode == 500 && resultCode == 1) {//查看详情
            getMsg();
            ((MybikeActivity6)getActivity()).f2.datafalsh();
        }else if (requestCode == 301 && resultCode == 1) {//第一次转赠
            getMsg();
            ((MybikeActivity6)getActivity()).f2.datafalsh();
        }else if (requestCode == 302 && resultCode == 101) {//不是第一次转赠
            getMsg();
            ((MybikeActivity6)getActivity()).f2.datafalsh();
        }
    }
    public void reflex(final TabLayout tabLayout){
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = Util. dp2px(tabLayout.getContext(), 30);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width ;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
